<?php

namespace app\admin\model;

use app\common\model\BaseModel;


class SubjectRecord extends BaseModel
{


    // 表名
    protected $name = 'subject_record';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'starttime_text',
        'endtime_text',
        'examtime_text'
    ];


    public function getList($where, $size = 12, $field = '*', $order = 'id desc',$with=[])
    {
        $list = $this->where(function ($query) use ($where) {
            if (isset($where['keywords'])&&$where['keywords']){
//                $query->where('userinfo.username',"like","%{$where['keywords']}%");
            }

        })
            ->with($with)
            ->field($field)
            ->order($order)
            ->paginate($size);
        //$list->each(function ($item) {
        //});
        return $list->toArray();

    }


    public function getuserList($where, $size = 12, $field = '*', $order = 'id desc',$with=[])
    {
        $list = $this->alias('record')->where(function ($query) use ($where) {
            if (isset($where['keywords'])&&$where['keywords']){
                $query->where('subject.name|user.username|counselor',"like","%{$where['keywords']}%");
            }
            if (isset($where['status'])&&$where['status']>-1){
                $query->where('record.status',$where['status']);
            }else{
                $query->where('record.status','in',[1,2,3]);
            }
            if (isset($where['user_id'])&&$where['user_id']){
                $query->where('record.user_id',$where['user_id']);
            }
        })
           ->leftJoin('user user','record.user_id = user.id')
           ->leftJoin('subject subject','record.subject_id = subject.id')

            ->field('record.*,user.username,subject.name,subject.subject_cate_id')
            ->order($order)
            ->paginate($size);
        //$list->each(function ($item) {
        //});
        return $list->toArray();

    }


    public function getStatusList()
    {
        return [ '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }



    /**
     * 关联用户
     * @return \think\model\relation\BelongsTo
     */
    public function userinfo()
    {
        return $this->belongsTo('User', 'user_id', 'id');
    }

    /**
     * 关联分享人
     * @return \think\model\relation\BelongsTo
     */
    public function quser()
    {
        return $this->belongsTo('\app\common\model\Quser', 'counselor_id', 'id');
    }


    /**
     * 关联题库
     * @return void
     */
    public function subject()
    {
        return $this->belongsTo('Subject', 'subject_id', 'id');
    }
    public function getStarttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['starttime']) ? $data['starttime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getExamtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['examtime']) ? $data['examtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setStarttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setExamtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }



}
